

import os
def list_files(startpath):########使用函数
    for root, dirs, files in os.walk(startpath):#########遍历
        level = root.replace(startpath, '').count(os.sep)
        dir_indent = "|   " * (level - 1) + "|-- "
        file_indent = "|   " * level + "|-- "
        if not level:
            print('.')
        else:
            print('{}{}'.format(dir_indent,os.path.basename(root)))
        for f in files:
            print('{}{}'.format(file_indent,f))
list_files(r'D:\python作业')
